package com.pirestupppai.ui.activity;

import android.content.Context;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.pirestupppai.R;
import com.pirestupppai.adapter.FragmentAdapter;
import com.pirestupppai.base.BaseActivity;
import com.pirestupppai.common.Constant;
import com.pirestupppai.rxbus.RxBus;
import com.pirestupppai.ui.fragment.UserAskFragment;
import com.pirestupppai.ui.fragment.UserDescFragment;
import com.pirestupppai.ui.fragment.UserInfoFragment;
import com.pirestupppai.ui.fragment.UserReplyFragment;
import com.pirestupppai.widgets.dialog.UserSelectMoreDialog;
import com.pirestupppai.widgets.helper.TabLayoutHelper;

import butterknife.BindView;
import rx.Subscription;
import rx.functions.Action1;

import static com.pirestupppai.rxbus.RxBus.RXBUS_REFRESH_USER_NAME;

/**
 * 其它用户个人中心
 *
 * @author: xiaoluo
 * @date: 2017-03-09 18:40
 */
public class UserActivity extends BaseActivity {
    private final static String TAG = UserActivity.class.getSimpleName();

    @BindView(R.id.user_tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.user_viewpager)
    ViewPager viewPager;
    @BindView(R.id.appbarlayout)
    AppBarLayout appBarLayout;
    @BindView(R.id.back_btn)
    ImageView backBtn;
    @BindView(R.id.user_more_iv)
    ImageView userMoreIv;
    @BindView(R.id.title_nickname)
    TextView titleNicknameTv;
    @BindView(R.id.user_info_viewpager)
    ViewPager infoViewpager;
    @BindView(R.id.dot_one)
    ImageView dotOne;
    @BindView(R.id.dot_two)
    ImageView dotTwo;

    private CollapsingToolbarLayoutState mState;  // 状态

    private Subscription mSubscription;

    private int mUid;

    @Override
    protected Context getActivityContext() {
        return this;
    }

    @Override
    protected int loadParentLayout() {
        return R.layout.activity_user;
    }

    @Override
    protected void initViews() {
        mUid = getIntent().getIntExtra(Constant.IntentKey.UID, -1);
        int mSelectItem = getIntent().getIntExtra(Constant.IntentKey.SELECT_ITEM, 0);

        FragmentAdapter mFragmentAdapter = new FragmentAdapter(getSupportFragmentManager());
        mFragmentAdapter.addFragment(new UserReplyFragment(mUid), "作品");
        mFragmentAdapter.addFragment(new UserAskFragment(mUid), "求P");
        viewPager.setAdapter(mFragmentAdapter);
        viewPager.setOffscreenPageLimit(1);
        // 将TabLayout与viewpager绑定
        tabLayout.setupWithViewPager(viewPager);
        viewPager.setCurrentItem(mSelectItem);

        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                TabLayoutHelper.setIndicatorWidth(tabLayout, 60, 60);
            }
        });

        FragmentAdapter mInfoAdapter = new FragmentAdapter(getSupportFragmentManager());
        mInfoAdapter.addFragment(new UserInfoFragment(mUid), "主要信息");
        mInfoAdapter.addFragment(new UserDescFragment(), "简介");
        infoViewpager.setAdapter(mInfoAdapter);
    }

    @Override
    protected void initListeners() {
        appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                if (verticalOffset == 0) {
                    if (mState != CollapsingToolbarLayoutState.EXPANDED) {
                        mState = CollapsingToolbarLayoutState.EXPANDED;   // 修改状态为展开
                        Log.d(TAG, "展开");
                    }
                } else if (Math.abs(verticalOffset) >= appBarLayout.getTotalScrollRange()) {
                    if (mState != CollapsingToolbarLayoutState.COLLAPSED) {
                        mState = CollapsingToolbarLayoutState.COLLAPSED;   // 修改状态为折叠
                        Log.d(TAG, "折叠");
                    }
                } else {
                    if (Math.abs(verticalOffset) >= appBarLayout.getTotalScrollRange() / 2) {
                        titleNicknameTv.setVisibility(View.VISIBLE);
                    } else {
                        titleNicknameTv.setVisibility(View.INVISIBLE);  // 为啥用GONE 毛玻璃也会消失?
                    }
                    if (mState != CollapsingToolbarLayoutState.INTERNEDIATE) {
                        mState = CollapsingToolbarLayoutState.INTERNEDIATE;  // 修改状态为中间
                        Log.d(TAG, "中间");
                    }
                }
            }
        });

        infoViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    dotOne.setImageResource(R.drawable.shape_select_color_black);
                    dotTwo.setImageResource(R.drawable.shape_scroll_unselect);
                } else {
                    dotOne.setImageResource(R.drawable.shape_scroll_unselect);
                    dotTwo.setImageResource(R.drawable.shape_select_color_black);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


        backBtn.setOnClickListener(this);
        userMoreIv.setOnClickListener(this);
    }

    @Override
    protected void initLogic() {
//        swipeRefreshLayout.post(new Runnable() {
//            @Override
//            public void run() {
//                swipeRefreshLayout.setRefreshing(true);
//                getUserData();
//            }
//        });
        //注册观察者
        mSubscription = RxBus.getInstance()
                .toObservable(RXBUS_REFRESH_USER_NAME, String.class)
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        titleNicknameTv.setText(s);
                    }
                });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_btn:
                finish();
                break;
            case R.id.user_more_iv:
                UserSelectMoreDialog userSelectMoreDialog = new UserSelectMoreDialog(mContext, mUid);
                userSelectMoreDialog.show();
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (!mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
        }
    }


    private enum CollapsingToolbarLayoutState {
        EXPANDED, COLLAPSED, INTERNEDIATE
    }
}
